package com.a14810100103szm.a7zuxiangmu.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.a14810100103szm.a7zuxiangmu.R;
import com.a14810100103szm.a7zuxiangmu.beans.LoginBean;
import com.a14810100103szm.a7zuxiangmu.listener.RetrofitListener;
import com.a14810100103szm.a7zuxiangmu.model.UserModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener,RetrofitListener<LoginBean> {
    private EditText username;
    private EditText userpass;
    private Button btn_login;
    private Button btn_register;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }

    public void initViews()
    {
        btn_login = (Button) findViewById(R.id.btn_login);
        username = (EditText) findViewById(R.id.et_user);
        userpass = (EditText) findViewById(R.id.et_passwd);
        btn_register=(Button) findViewById(R.id.btn_register);
    }
    /*  public void  initEvents(){
      btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

    }*/

    public void initEvents()
    {
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View view)
    {
        switch (view.getId())
        {
            case R.id.btn_login:
                //处理登录的点击事件
                if ("".equals(username.getText().toString())||"".equals(username.getText().toString()))
                    Toast.makeText(LoginActivity.this,"用户名或密码不能为空",Toast.LENGTH_LONG).show();
                else
                    login();
                break;
            case R.id.btn_register:
            Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivityForResult(intent,1);
               // break;
        }
    }

    public void login()
    {
        UserModel userModel=new UserModel();
        userModel.userLogin(username.getText().toString(),userpass.getText().toString(),this);
    }

    public void saveUserInfo(String user_id)
    {
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username", username.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", userpass.getText().toString());
        editor.commit();//提交修改
    }

    @Override
    public void onSuccess(LoginBean userId, int flag) {
        if (userId.getUserid().equals("0")) {
            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
        } else {
            saveUserInfo(userId.getUserid());
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }
    protected void onActivityResult(int requestCode,int resultCode,Intent data)
    {
        super.onActivityResult(requestCode,resultCode,data);
        String et_user=data.getStringExtra("user");
        username.setText(et_user);
    }
    @Override
    public void onFail() {
        Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
    }

   /* public String getUser_id()
    {
        SharedPreferences sp;
        sp=getSharedPreferences("userInfo",Context.MODE_PRIVATE);
        return sp.getString("user_id";"");
    }*/


}
